package cn.huyanbing.middleware.thread.research;

/**
 * @BelongProject middleware
 * @BelongPackage cn.huyanbing.middleware.thread.research
 * @Description  java 多线程 可见性调研
 * @ClassName Volatile
 * @Author huyangbing
 * @Date 2024/10/21 20:52
 * @Version 1.0.0
 */
public class Volatile {
    private static long count = 0;

    private void add10k() {
        int idx = 0;
        while (idx++ < 10000) {
            count += 1;
        }
    }

    public static long calc() throws InterruptedException {
        final Volatile test = new Volatile();
        //创建两个线程，执行add()操作
        Thread th1 = new Thread(() -> {
            test.add10k();
        });

        Thread th2 = new Thread(() -> {
            test.add10k();
        });

        // 启动两个线程
        th1.start();
        th2.start();

        //等待两个线程执行结束
        th1.join();
        th2.join();

        return count;
    }


    public static void main(String[] args) throws InterruptedException {

        calc();

        System.out.println(count);
    }


}
